package com.kfm.base.stream;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

public class Demo {


    public static String concat(String[] strings) {
        if (strings == null) {
            throw new NullPointerException();
        }
//        return String.join("", strings);

        Stream<String> stream = Arrays.stream(strings);
//
//        StringBuilder sb = new StringBuilder();
//        stream.forEach(e -> sb.append(e));
//        return sb.toString();

        // Collectors.joining()  stream 元素拼接
//        String collect = stream.collect(Collectors.joining());
//
//        return collect;

        String reduce = stream
                .reduce("", (a, b) -> a + b);

        return reduce;
    }

    public static void compute(Collection<Integer> collection) {
//        Integer sum = collection.stream()
//                .reduce(0, (a, b) -> a + b);
//        collection.stream()
////                .max((a, b) -> a - b)
//                .min((a, b) -> a - b)
//                .get();

        int sum = collection.stream()
                .mapToInt(e -> e)
//                .sum();
//                .max()
                .min()
                .getAsInt();

        System.out.println(sum);
    }

    public static int count(String[] strings) {
        int sum = Arrays.stream(strings)
                .mapToInt(e -> e.length())
                .sum();
        return sum;
    }

    public static void employee(Collection<Employee> collection) {

        collection.stream()
                .sorted( (a, b) -> (int)(a.getSalary() - b.getSalary()))
                .limit(3)
                .forEach(e -> System.out.println(e));

    }

    public static void avg(Collection<Integer> collection){
        double asDouble = collection.stream()
                .mapToInt(e -> e * e)
                .average()
                .getAsDouble();

        System.out.println(asDouble);
    }

    public static void main(String[] args) {
//        String[] strs = {"hello", "world", "java"};
//        System.out.println(concat(strs));

        List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9);

        avg(list);

//        compute(list);

//        List<Employee> list = List.of(
//                new Employee("张三", 3000.0),
//                new Employee("李四", 3500.0),
//                new Employee("王五", 2500.1),
//                new Employee("赵六", 2500.0),
//                new Employee("钱七", 5000.0)
//        );
//
//        employee(list);
    }
}

class Employee implements Comparable<Employee>{

    private String name;

    private Double salary;

    public Employee(String name, Double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                '}';
    }

    @Override
    public int compareTo(Employee o) {
        return this.salary - o.salary == 0.0 ? 0 : this.salary - o.salary > 0? 1 : -1;
    }
}
